package com.boesch.electroOffice.presentation.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.business.birt.BerichtResource;
import com.boesch.electroOffice.business.businessObject.ReportBusinessObject;
import com.boesch.electroOffice.business.common.BusinessException;
import com.boesch.electroOffice.model.birt.ReportOptions;
import com.boesch.electroOffice.model.enums.ReportFileNames;
import com.boesch.electroOffice.model.enums.SessionParameter;
import com.boesch.electroOffice.model.enums.SubfunctionIdentifier;
import com.boesch.electroOffice.utils.LanguageUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.boesch.electroOffice.utils.UserUtils;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

@Controller
@ManagedBean
@SessionScoped
public class ReportController implements Serializable {
	
	private static final long serialVersionUID = -1478335623471608501L;
	private static final Log log = LogFactoryUtil.getLog(ReportController.class);
	
	@Autowired
	private ReportBusinessObject reportBusinessObject;
	
	private static String FILE_FORMAT_PDF = "pdf";
	
	private String relativeFilePath = "";
	
	public void createReport() {
		relativeFilePath = "";
		String key = PortletUtils.getSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue());
		try {
			if(key.equals(SubfunctionIdentifier.ACHIEVEMENT_CURRENT_MONTH.getValue())) {
				relativeFilePath = createReportAchievementCurrentMonth();
			} else if(key.equals(SubfunctionIdentifier.ACHIEVEMENT_LAST_MONTH.getValue())) {
				relativeFilePath = createReportAchievementLastMonth();
			} else if(key.equals(SubfunctionIdentifier.ACHIEVEMENT_CURRENT_YEAR.getValue())) {
				relativeFilePath = createReportAchievementCurrentYear();
			} else if(key.equals(SubfunctionIdentifier.APPOINTMENT_PDF.getValue())) {
				relativeFilePath = createReportAppointment();
			} else if(key.equals(SubfunctionIdentifier.INVOICE_ABRIDGEMENT_REPORT.getValue())) {
				relativeFilePath = createReportInvoiceOverview();
			} else if(key.equals(SubfunctionIdentifier.OPEN_INVOICES_REPORT.getValue())) {
				relativeFilePath = createReportOpenInvoices();
			} else if(key.equals(SubfunctionIdentifier.PAYED_INVOICES_REPORT.getValue())) {
				relativeFilePath = createReportPayedInvoices();
			} else if(key.equals(SubfunctionIdentifier.SALARY_LIST_STAFF.getValue())) {
				relativeFilePath = createReportSalaryList();
			}
		} catch(BusinessException e) {
			log.error("Failed to create pdf report!", e);
		} catch(Exception e) {
			log.error("Failed to create pdf report!", e);
		}
	}
	
	public String getRelativeFilePath() {
		return relativeFilePath;
	}
	
	private String createReportAchievementCurrentMonth() throws BusinessException {
		String fileName = ReportFileNames.ACHIEVEMENT_CURRENT_MONTH.getValue();
		String userId = String.valueOf(UserUtils.getCurrentUser().getUserId());
		Locale locale = LanguageUtils.getLocale();
		Map<String, String> params = new HashMap<String, String>();
		params.put("userId", userId);
		return createReportAndGetPath(FILE_FORMAT_PDF, fileName, params, locale);
	}
	
	private String createReportAchievementLastMonth() throws BusinessException {
		String fileName = ReportFileNames.ACHIEVEMENT_LAST_MONTH.getValue();
		String userId = String.valueOf(UserUtils.getCurrentUser().getUserId());
		Locale locale = LanguageUtils.getLocale();
		Map<String, String> params = new HashMap<String, String>();
		params.put("userId", userId);
		return createReportAndGetPath(FILE_FORMAT_PDF, fileName, params, locale);
	}
	
	private String createReportAchievementCurrentYear() throws BusinessException {
		String fileName = ReportFileNames.ACHIEVEMENT_CURRENT_YEAR.getValue();
		String userId = String.valueOf(UserUtils.getCurrentUser().getUserId());
		Locale locale = LanguageUtils.getLocale();
		Map<String, String> params = new HashMap<String, String>();
		params.put("userId", userId);
		return createReportAndGetPath(FILE_FORMAT_PDF, fileName, params, locale);
	}
	
	private String createReportAppointment() throws BusinessException {
		String fileName = ReportFileNames.APPOINTMENT.getValue();
		String appointmentId = PortletUtils.getSessionAttribute(SessionParameter.APPOINTMENT_ID.getValue());
		Locale locale = LanguageUtils.getLocale();
		Map<String, String> params = new HashMap<String, String>();
		params.put(SessionParameter.APPOINTMENT_ID.getValue(), appointmentId);
		return createReportAndGetPath(FILE_FORMAT_PDF, fileName, params, locale);
	}
	
	private String createReportInvoiceOverview() throws BusinessException {
		String fileName = ReportFileNames.INVOICE_OVERVIEW.getValue();
		Locale locale = LanguageUtils.getLocale();
		return createReportAndGetPath(FILE_FORMAT_PDF, fileName, null, locale);
	}
	
	private String createReportOpenInvoices() throws BusinessException {
		String fileName = ReportFileNames.OPEN_INVOICES.getValue();
		Locale locale = LanguageUtils.getLocale();
		return createReportAndGetPath(FILE_FORMAT_PDF, fileName, null, locale);
	}
	
	private String createReportPayedInvoices() throws BusinessException {
		String fileName = ReportFileNames.PAYED_INVOICES.getValue();
		Locale locale = LanguageUtils.getLocale();
		return createReportAndGetPath(FILE_FORMAT_PDF, fileName, null, locale);
	}
	
	private String createReportSalaryList() throws BusinessException {
		String fileName = ReportFileNames.SALARY_LIST.getValue();
		Locale locale = LanguageUtils.getLocale();
		return createReportAndGetPath(FILE_FORMAT_PDF, fileName, null, locale);
	}
	
	private String createReportAndGetPath(String fileFormat, String fileName, 
			Map<String, String> params, Locale locale) throws BusinessException {
		BerichtResource resource = loadBericht(fileFormat, fileName, params, locale);
		log.debug("BerichtResource: " + resource.toString());
		String relativePath = resource.generateReport();
		return relativePath;
	}

	private BerichtResource loadBericht(String fileFormat, String fileName, 
			Map<String, String> params, Locale locale) {
		if (null != reportBusinessObject) {
			ReportOptions options = new ReportOptions();
			options.setOutFormat(fileFormat);
			options.setReportName(fileName);
			options.setParameters(params);
			options.setLocale(locale);
			return new BerichtResource(options, reportBusinessObject);
		}
		return new BerichtResource(null, null);
	}
}
